<style>
  .layui-table-cell .layui-table-sort {
    display: none;
  }

  .layui-table-view .layui-table-tool-self {
    display: flex;
    justify-content: flex-end;
    position: initial;
    margin-top: 12px;
  }
</style>

<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form lay-header">
        <div class="toolbar">
          <div class="layui-form-item lay-query-content">
            <div class="lay-query-box">
              <div class="query-lt">
                <div class="layui-inline">
                  <label class="layui-form-label">出库单号：</label>
                  <div class="layui-input-inline">
                    <input
                      name="workOrderCode"
                      id="workOrderCode"
                      class="layui-input"
                      type="text"
                      placeholder="请输入出库单号"
                      autocomplete="off"
                    />
                  </div>
                </div>
              </div>

              <div class="query-rt">
                <div class="layui-inline">
                  <button
                    class="layui-btn icon-btn color-reseda"
                    lay-filter="formSubSearchLog"
                    lay-submit
                    layui-form-keyDownSearch="true"
                  >
                    查询
                  </button>
                  <button
                    class="layui-btn icon-btn color-white"
                    lay-filter="resets"
                    lay-submit
                  >
                    重置
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <button
                id="receiptRecordAdd"
                type="button"
                class="layui-btn icon-btn"
              >
                添加其他出库
              </button>
            </div>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table-bor">
        <table
          class="layui-table"
          id="receiptRecordTable"
          lay-filter="receiptRecordTable"
        ></table>
      </div>
    </div>
  </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="receiptRecordTabBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail"
    >详情</a
  >
  {{# if(d.status == '1'){ }}
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="audit">审核</a>
  {{# } }}
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="receiptRecordForm">
  <form
    lay-filter="receiptRecordForm"
    id="receiptRecordFormContent"
    class="layui-form open-form"
  >
    <div class="open-model">
      <div class="open-auto">
        <input name="id" type="hidden" />
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
              <select
                id="inventoryTypeId"
                name="inventoryTypeId"
                lay-filter="inventoryTypeId"
                disabled
                lay-search
              ></select>
            </div>
          </div>
        </div>

        <!-- <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">仓库类型</label>
            <div class="layui-input-block">
              <select id="warehouseType" name="warehouseType" lay-filter="warehouseType"></select>
            </div>
          </div>
        </div>

        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">仓库</label>
            <div class="layui-input-block">
              <select id="receiptWarehouse" name="receiptWarehouse" lay-filter="receiptWarehouse" lay-search></select>
            </div>
          </div>
        </div>-->

        <div class="layui-col-xs4" id="creatorNameDiv">
          <div class="layui-form-item">
            <label class="layui-form-label">出库人</label>
            <div class="layui-input-block">
              <input
                id="creatorName"
                name="creatorName"
                type="text"
                class="layui-input"
                readonly
              />
            </div>
          </div>
        </div>

        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">审核人</label>
            <div class="layui-input-block">
              <select
                id="auditPerson"
                name="auditPerson"
                lay-filter="auditPerson"
                lay-search
              ></select>
            </div>
          </div>
        </div>

        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
              <input
                id="remark"
                name="remark"
                type="text"
                class="layui-input"
              />
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="addReceiptRecordDetailForm-title">
            <h3 class="title">其他出库物料明细</h3>
            <button
              class="layui-btn addTable addReceiptRecordDetail"
              type="button"
              style="margin-top: 10px;float: right;margin-right: 20px;"
            >
              添加物料
            </button>
          </div>
          <div class="line"></div>
          <!-- 表格 -->
          <table
            class="layui-table"
            id="receiptRecordDetailTable"
            lay-filter="receiptRecordDetailTable"
          ></table>
        </div>
      </div>
      <div class="layui-form-item text-right">
        <button
          class="layui-btn layui-btn-primary"
          type="button"
          ew-event="closeDialog"
        >
          取消
        </button>
        <button
          class="layui-btn"
          lay-filter="receiptRecordFormSubmit"
          lay-submit
        >
          保存
        </button>
      </div>
    </div>
  </form>
</script>

<!--领料明细表格操作列-->
<script type="text/html" id="receiptRecordDetailTableBar">
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 领料记录审核、详情弹窗 -->
<script type="text/html" id="receiptRecordEditFormQiTa">
  <form lay-filter="receiptRecordEditFormQiTa" class="layui-form open-form">
    <div class="open-model">
      <div class="open-auto">
        <input name="id" type="hidden" />
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-block">
              <input
                name="inventoryTypeName"
                type="text"
                class="layui-input"
                readonly
              />
            </div>
          </div>
        </div>
        <!--   <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">开工单编号</label>
            <div class="layui-input-block">
              <input name="workOrderCode" type="text" class="layui-input" readonly />
            </div>
          </div>
        </div>-->
        <!--  <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">工站任务编号</label>
            <div class="layui-input-block">
              <input name="productionProcessDeviceCode" type="text" class="layui-input" readonly />
            </div>
          </div>
        </div>-->

        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">料号数</label>
            <div class="layui-input-block">
              <input
                name="receiptNum"
                type="text"
                class="layui-input"
                readonly
              />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">审核人</label>
            <div class="layui-input-block">
              <input
                name="auditPersonName"
                type="text"
                class="layui-input"
                readonly
              />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">出库人</label>
            <div class="layui-input-block">
              <input
                name="creatorName"
                type="text"
                class="layui-input"
                readonly
              />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
              <input name="remark" type="text" class="layui-input" />
            </div>
          </div>
        </div>
        <div class="layui-col-xs4">
          <div class="layui-form-item">
            <label class="layui-form-label">审核结果</label>
            <div class="layui-input-block">
              <textarea
                name="auditResult"
                placeholder="请输入审核结果"
                maxlength="200"
                class="layui-textarea"
              ></textarea>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <div class="addReceiptRecordDetailForm-title" style="margin-left: 0;">
            <h3 class="title">其他出库明细</h3>
          </div>
          <div class="line"></div>
          <!-- 表格 -->
          <table
            class="layui-table"
            id="receiptRecordDetailQiTaEditTable"
            lay-filter="receiptRecordDetailQiTaEditTable"
          ></table>
        </div>
      </div>
      <div class="layui-form-item text-right" id="receiptRecordFormAuditDiv">
        <button
          class="layui-btn"
          lay-filter="auditSuccessBtn"
          type="button"
          lay-submit
        >
          通过
        </button>
        <button
          class="layui-btn layui-btn-danger"
          lay-filter="auditFailBtn"
          type="button"
          lay-submit
        >
          驳回
        </button>
      </div>
    </div>
  </form>
</script>

<!--物料弹窗-->
<script type="text/html" id="productDetailForm">
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
        <!-- 头部操作栏 -->
        <div class="layui-form toolbar">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label w-auto">物料类别</label>
              <div class="layui-input-inline">
                <input id="productTypeId" type="hidden" name="productTypeId" />
                <div id="selectProductType"></div>
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label w-auto">物料编码</label>
              <div class="layui-input-inline mr0" style="width: 240px;">
                <input
                  name="skuCode"
                  class="layui-input"
                  type="text"
                  placeholder="请输入物料编码"
                />
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label w-auto">物料规格</label>
              <div class="layui-input-inline mr0" style="width: 240px;">
                <input
                  name="skuSpec"
                  class="layui-input"
                  type="text"
                  placeholder="请输入物料规格"
                />
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label w-auto">名称</label>
              <div class="layui-input-inline mr0" style="width: 240px;">
                <input
                  name="skuName"
                  class="layui-input"
                  type="text"
                  placeholder="请输入物料名称"
                />
              </div>
            </div>
            <div class="layui-inline">
              <button
                class="layui-btn icon-btn"
                lay-filter="productDetailSearch"
                lay-submit
                style="padding: 0 24px;"
              >
                搜索
              </button>
              <button
                class="layui-btn icon-btn"
                lay-filter="productDetailResets"
                lay-submit
                style="padding: 0 24px;"
              >
                重置
              </button>
              <button id="productDetailConfirm" class="layui-btn icon-btn">
                确认选择
              </button>
            </div>
          </div>
        </div>
        <!-- 表格 -->
        <div class="scroll-table-4-5">
          <table
            class="layui-table"
            id="productDetailTable"
            lay-filter="productDetailTable"
          ></table>
        </div>
      </div>
    </div>
  </div>
</script>
<!-- js部分 -->
<script>
  layui.use(
    [
      "layer",
      "form",
      "table",
      "laydate",
      "admin",
      "laytpl",
      "tableTreeDj",
      "config",
    ],
    function () {
      var $ = layui.jquery;
      var layer = layui.layer;
      var laydate = layui.laydate;
      var table = layui.table;
      var form = layui.form;
      var config = layui.config;
      var admin = layui.admin;
      var tableTree = layui.tableTreeDj;

      //所属物料列表
      admin.initDataSelect(
        "enterprise/product/admin/productDetail/list",
        null,
        "productDetailId",
        null,
        "所属物料",
        "skuName",
        null
      );
      //物料选择事件-所属工序列表
      form.on("select(productDetailId)", function (selectData) {
        $("#processId").empty();
        admin.initDataSelect(
          "enterprise/echnologyParam/admin/echnologyParam/list",
          { productDetailId: selectData.value },
          "processId",
          null,
          "所属工序",
          null,
          null
        );
      });

      // 渲染表格
      var insTb = table.render({
        id: "#receiptRecordTable",
        elem: "#receiptRecordTable",
        url:
          config.base +
          "enterprise/receiptRecord/admin/receiptRecordOther/list",
        page: true,
        cellMinWidth: 100,
        cols: [
          [
            { type: "numbers", title: "序号", align: "center" },
            { field: "code", title: "出库单编号", align: "center", width: 160 },
            /* { field: "workOrderCode", title: "开工单编号", align: "center", width: 150 },
          { field: "productionPlanRecordCode", title: "制造令编号", align: "center", width: 150 },
          { field: "productionPlanCode", align: "center", title: "所属计划", width: 150 },
   ,*/
            /*      { field: "warehouseTypeName", align: "center", title: "仓库类型" },
          { field: "receiptWarehouseName", align: "center", title: "仓库" },*/
            { field: "receiptNum", align: "center", title: "料号数" },
            { field: "statusName", align: "center", title: "状态" },
            { field: "auditPersonName", align: "center", title: "审核人" },
            { field: "creatorName", align: "center", title: "申请人" },
            { field: "createTime", align: "center", title: "时间", width: 160 },
            {
              align: "center",
              toolbar: "#receiptRecordTabBar",
              title: "操作",
              width: 160,
            },
          ],
        ],
        done: function (res, curr, count) {},
      });

      let materialData = [];

      //初始化产品类别
      admin.initDataSelectTree(
        true,
        "enterprise/product/admin/productType/getTreeData",
        { limit: 99999 },
        "productType",
        null,
        "产品类别",
        "productTypeId",
        function (row) {
          $("input[name='productTypeId']").val(row.arr[0].value);
        }
      );

      //渲染下拉
      form.render("select");

      // 工具条点击事件
      table.on("tool(receiptRecordTable)", function (obj) {
        var data = obj.data;
        if (obj.event === "audit") {
          //审核
          showModel(data, 1);
        } else if (obj.event === "detail") {
          //详情
          showModel(data, 2);
        }
      });

      //监听排序
      table.on("sort(receiptRecordTable)", function (obj) {
        table.reload("receiptRecordTable", {
          initSort: obj,
          where: {
            sort: obj.field,
            order: obj.type,
          },
        });
      });

      // 搜索
      form.on("submit(formSubSearchLog)", function (data) {
        insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
      });

      // 重置搜索
      form.on("submit(resets)", (data) => {
        insTb.reload(
          { where: admin.resetSearch(data.field), page: { curr: 1 } },
          "data"
        );
      });

      // 添加按钮点击事件
      $("#receiptRecordAdd").click(function () {
        showEditModel();
      });

      // 领料记录弹窗
      function showEditModel() {
        admin.open({
          type: 1,
          area: ["1500px", "90%"],
          style: "font-size: 20px;",
          title: "添加其他出库记录",
          content: $("#receiptRecordForm").html(),
          success: async function () {
            //领料人
            $("#creatorNameDiv").hide();
            //仓库类型
            admin.initDataSelect(
              "enterprisecab/warehouse/admin/warehouseType/list",
              { limit: 99999 },
              "warehouseType",
              null,
              "仓库类型",
              null,
              null
            );

            //初始化领料类型
            // 默认为领料出库 valye为5
            admin.initDataSelect(
              "enterprisecab/warehouse/admin/warehouseInventoryType/list",
              { limit: 99999 },
              "inventoryTypeId",
              14,
              "领料类型",
              null,
              null
            );

            //审核人
            admin.initDataSelect(
              "user/admin/user/list",
              { limit: 99999 },
              "auditPerson",
              null,
              "审核人",
              "nickname",
              null
            );

            //开工单编号选择事件
            //  form.on("select(workOrderId)", function (selectData) {
            //    admin.req("production/productionPlan/admin/productionWorkOrder/getWorkOrderDetails", { workOrderId: selectData.value }, function (res) {
            //      $("#productionProcessDeviceCode").val(res.data.productionProcessDeviceCode);
            //      $("#planCode").val(res.data.planCode);
            //    });
            //  });

            //仓库类型选择事件
            form.on("select(warehouseType)", function (selectData) {
              $("#receiptWarehouse").empty();
              admin.initDataSelect(
                "enterprisecab/warehouse/admin/warehouse/list",
                {
                  limit: 99999,
                  warehouseTypeId: selectData.value,
                },
                "receiptWarehouse",
                null,
                "领料仓库",
                null,
                null
              );
            });

            //领料明细表格
            table.render({
              elem: "#receiptRecordDetailTable",
              data: [],
              cellMinWidth: 100,
              page: false,
              cols: [
                [
                  { type: "numbers", width: 50, title: "序号" },
                  { field: "skuCode", title: "物料编码", align: "center" },
                  {
                    field: "skuName",
                    title: "物料名称",
                    align: "center",
                    width: 150,
                  },
                  {
                    field: "skuSpec",
                    title: "物料规格",
                    align: "center",
                    width: 150,
                  },
                  {
                    field: "materialNatureName",
                    title: "物料类别性质",
                    align: "center",
                    width: 140,
                  },
                  {
                    field: "productTypeName",
                    title: "物料类别",
                    align: "center",
                    width: 140,
                  },
                  { field: "baseUnitName", title: "基础单位", align: "center" },
                  { field: "unitName", title: "计量单位", align: "center" },
                  {
                    field: "mainUnitName",
                    title: "主计量单位",
                    align: "center",
                  },
                  {
                    field: "receiptNum",
                    title: "出库数量",
                    align: "center",
                    width: 140,
                    templet: admin.setTableInput("receiptNum"),
                  },
                  {
                    field: "remark",
                    title: "备注",
                    align: "center",
                    width: 160,
                    edit: "text",
                  },
                  {
                    align: "center",
                    toolbar: "#receiptRecordDetailTableBar",
                    title: "操作",
                    width: 100,
                  },
                ],
              ],
              done: () => {
                admin.tableInputChange("receiptRecordDetailTable");
              },
            });

            // 领料明细工具条点击事件
            table.on("tool(receiptRecordDetailTable)", function (obj) {
              let rowData = obj.data;
              let list = materialData;
              if (obj.event === "save") {
                // 保存
                let planProductionQuantity = $(this)
                  .parents("td")
                  .siblings()
                  .find(".planProductionQuantity")
                  .val();
                let outputQuantity = $(this)
                  .parents("td")
                  .siblings()
                  .find(".outputQuantity")
                  .val();
                if (planProductionQuantity == "") {
                  layer.msg("请输入计划排产数量", { icon: 2 });
                  return false;
                }
                if (outputQuantity == "") {
                  layer.msg("请输入排产数量", { icon: 2 });
                  return false;
                }
                list.forEach((item, index) => {
                  if (rowData.deviceCode === item.deviceCode) {
                    list[index].status = "yes";
                    list[index].outputQuantity = outputQuantity;
                    list[index].planProductionQuantity = planProductionQuantity;
                  }
                });
              } else if (obj.event === "del") {
                let list1=table.cache['receiptRecordDetailTable']
                console.log(list1)
                //删除
                list1.forEach((item, index) => {
                  if (rowData.skuCode === item.skuCode) {
                    list1.splice(index, 1);
                  }
                });
                //删除后更新数据
                list = list1;
                materialData = list1;
              }
              // 渲染表格
              table.reload("receiptRecordDetailTable", {
                data: list,
              });
            });

            // 添加领料信息
            $(".addReceiptRecordDetail").click(function () {
              let arrList = [];
              materialData.forEach((item) => {
                arrList.push(item.skuCode);
              });
              var productDetailInsTb = admin.open({
                type: 1,
                title: "物料列表",
                area: "75%",
                offset: "65px",
                content: $("#productDetailForm").html(),
                success: function () {
                  console.log(form.val("receiptRecordForm"));
                  //物料类别
                  admin.initDataSelectTree(
                    true,
                    "enterprise/product/admin/productType/getTreeData",
                    { limit: 99999 },
                    "selectProductType",
                    null,
                    "物料类别",
                    "productTypeId",
                    null
                  );
                  // 渲染表格
                  var productDetailTable = table.render({
                    elem: "#productDetailTable",
                    url:
                      config.base +
                      "production/productionRecord/admin/productionRecord/getMaterialOthers",
                    where: {},
                    // url: config.base + 'enterprise/product/admin/productDetail/list',
                    page: true,
                    cellMinWidth: 100,
                    cols: [
                      [
                        { type: "checkbox" },
                        {
                          field: "productDetailId",
                          title: "物料ID",
                          hide: true,
                        },
                        {
                          field: "skuCode",
                          title: "物料编码",
                          align: "center",
                        },
                        {
                          field: "skuName",
                          title: "物料名称",
                          align: "center",
                        },
                        {
                          field: "skuSpec",
                          title: "物料规格",
                          align: "center",
                        },
                        {
                          field: "haveQuantity",
                          title: "库存数",
                          align: "center",
                        },
                        {
                          field: "materialNatureName",
                          title: "物料类别性质",
                          align: "center",
                        },
                        {
                          field: "productTypeName",
                          title: "物料类别",
                          align: "center",
                        },
                        {
                          field: "unitName",
                          title: "计量单位",
                          align: "center",
                        },
                        {
                          field: "baseUnitName",
                          title: "基础单位",
                          align: "center",
                        },
                        {
                          field: "mainUnitName",
                          title: "主计量单位",
                          align: "center",
                        },
                        {
                          field: "unitName",
                          title: "计量单位",
                          align: "center",
                        },
                        {
                          field: "createTime",
                          title: "创建时间",
                          align: "center",
                          width: 170,
                        },
                      ],
                    ],
                    done: function (res, curr, count) {
                      // 如果选择过的checkbox 则通过code标识判断 禁用下拉框
                      let allCheck = true;
                      for (let item in res.data) {
                        if (arrList.indexOf(res.data[item].skuCode) !== -1) {
                          allCheck = false;
                        }
                        break;
                      }
                      if (!allCheck) {
                        $(".layui-table-header")
                          .find(
                            "input[name = 'layTableCheckbox'][lay-filter='layTableAllChoose']"
                          )
                          .each(function () {
                            $(this)
                              .attr("disabled", "disabled")
                              .next()
                              .removeClass("layui-form-checked");
                            form.render("checkbox");
                          });
                      }
                      let i = 0;
                      $(".layui-table-body.layui-table-main")
                        .find("input[name='layTableCheckbox']")
                        .each(function () {
                          if (arrList.indexOf(res.data[i].skuCode) != -1) {
                            $(this)
                              .attr("disabled", "disabled")
                              .removeAttr("checked");
                            $(this).parents("tr").addClass("disabledBg");
                            form.render("checkbox");
                          }
                          i++;
                        });
                      i = 0;
                      $(".layui-table-fixed.layui-table-fixed-l")
                        .find(".layui-table-body")
                        .find("input[name='layTableCheckbox']")
                        .each(function () {
                          if (arrList.indexOf(res.data[i].skuCode) != -1) {
                            $(this)
                              .attr("disabled", "disabled")
                              .removeAttr("checked");
                            $(this).parents("tr").addClass("disabledBg");
                            form.render("checkbox");
                          }
                          i++;
                        });
                    },
                  });

                  // 搜索
                  form.on("submit(productDetailSearch)", function (data) {
                    productDetailTable.reload(
                      {
                        where: data.field,
                        page: { curr: 1 },
                      },
                      "data"
                    );
                  });

                  // 重置搜索
                  form.on("submit(productDetailResets)", (data) => {
                    $(".lay-header").find("select").val("");
                    $(".lay-header").find("input").val("");
                    this.$refs.elTree.setCheckedKeys([]);
                    let field = data.field;
                    for (let key in field) {
                      field[key] = "";
                    }
                    productDetailTable.reload(
                      { where: field, page: { curr: 1 } },
                      "data"
                    );
                  });

                  // 确认选择
                  $("#productDetailConfirm").click(function () {
                    let checkStatus = table.checkStatus("productDetailTable");
                    let productDetailCheckData = checkStatus.data;

                    if (productDetailCheckData.length == 0) {
                      layer.msg("请选择物料", { icon: 2, time: 1000 });
                      return;
                    }

                    let obj = materialData;
                    // 获取当前表格数据
                    let list = JSON.parse(JSON.stringify(materialData)); // 深拷贝原始数据，防止污染源数据

                    // 遍历选中的物料，逐个处理
                    productDetailCheckData.forEach((item) => {
                      // 判断是否已经存在该物料（根据 skuCode 判断）
                      const exists = list.some(
                        (i) => i.skuCode === item.skuCode
                      );

                      if (!exists) {
                        // 如果不存在，则添加一个新的物料对象
                        list.push({
                          productDetailId: item.productDetailId,
                          skuCode: item.skuCode,
                          skuName: item.skuName,
                          skuSpec: item.skuSpec,
                          materialNatureName: item.materialNatureName,
                          productTypeName: item.productTypeName,
                          unitName: item.unitName,
                          baseUnitName: item.baseUnitName,
                          mainUnitName: item.mainUnitName,
                          createTime: item.createTime,
                          receiptNum: item.receiptNum, // 空字符串或 0，保持用户后续可编辑
                          remark: item.remark, // 同样保留为空或默认值
                        });
                      }
                    });

                    // 更新全局变量
                    materialData = list;
                    // 关闭弹窗
                    layer.close(productDetailInsTb);

                    // 更新领料数
                    $("#receiptNum").val(list.length);

                    // 重新加载表格，保留原有字段（包括用户填写的 receiptNum、remark）
                    table.reload("receiptRecordDetailTable", {
                      data: list,
                    });
                  });
                },
              });
            });
          },
          finish: function () {
            table.reload("receiptRecordTable", {});
          },
        });
      }

      // 领料记录审核、详情弹窗
      function showModel(data, type) {
        admin.open({
          type: 1,
          area: ["1500px", "90%"],
          style: "font-size: 20px;",
          title: type === 1 ? "出库记录审核" : "出库记录详情",
          content: $("#receiptRecordEditFormQiTa").html(),
          success: function () {
            //详情页面-隐藏按钮
            if (type === 2) {
              $("#receiptRecordFormAuditDiv").hide();
            }
            //领料记录数据回显
            form.val("receiptRecordEditFormQiTa", data);
            form.render();
            //领料明细表格
            table.render({
              elem: "#receiptRecordDetailQiTaEditTable",
              data: data.receiptRecordDetailList,
              cellMinWidth: 100,
              page: false,
              cols: [
                [
                  { type: "numbers", width: 50, title: "序号" },
                  { field: "skuCode", title: "物料编码", align: "center" },
                  {
                    field: "skuName",
                    title: "物料名称",
                    align: "center",
                    width: 150,
                  },
                  {
                    field: "skuSpec",
                    title: "物料规格",
                    align: "center",
                    width: 150,
                  },
                  {
                    field: "materialNatureName",
                    title: "物料类别性质",
                    align: "center",
                    width: 140,
                  },
                  {
                    field: "productTypeName",
                    title: "物料类别",
                    align: "center",
                    width: 140,
                  },
                  { field: "baseUnitName", title: "基础单位", align: "center" },
                  { field: "unitName", title: "计量单位", align: "center" },
                  {
                    field: "receiptNum",
                    title: "出库数量",
                    align: "center",
                    width: 140,
                    edit: "text",
                  },
                  {
                    field: "remark",
                    title: "备注",
                    align: "center",
                    width: 160,
                    edit: "text",
                  },
                ],
              ],
            });

            //审核通过
            form.on("submit(auditSuccessBtn)", function (data) {
              data.field.status = 2; //待领料
              // 更新
              admin.req(
                "enterprise/receiptRecord/admin/receiptRecordOther/update",
                data.field,
                function (res) {
                  layer.closeAll("loading");
                  if (res.code == 200) {
                    layer.msg(res.msg, { icon: 1 });
                    layer.closeAll();
                    insTb.reload("receiptRecordTable");
                  } else {
                    layer.msg(res.msg, { icon: 2 });
                  }
                },
                "POST"
              );
              return false;
            });

            //审核驳回
            form.on("submit(auditFailBtn)", function (data) {
              data.field.status = 4; //驳回
              // 更新
              admin.req(
                "enterprise/receiptRecord/admin/receiptRecord/update",
                data.field,
                function (res) {
                  layer.closeAll("loading");
                  if (res.code == 200) {
                    layer.msg(res.msg, { icon: 1 });
                    layer.closeAll();
                    insTb.reload("receiptRecordTable");
                  } else {
                    layer.msg(res.msg, { icon: 2 });
                  }
                },
                "POST"
              );
              return false;
            });
          },
          finish: function () {
            table.reload("receiptRecordTable", {});
          },
        });
      }

      // 提交数据
      form.on("submit(receiptRecordFormSubmit)", function (d) {
        let flag = false;
        if (materialData.length == 0) {
          layer.msg("请添加出库信息", { icon: 2 });
          return false;
        }
        //领料明细列表
        var detailList = table.cache["receiptRecordDetailTable"];
        detailList.forEach((detail) => {
          if (
            detail.receiptNum === "" ||
            detail.receiptNum === null ||
            detail.receiptNum === undefined
          ) {
            layer.msg(`物料【${detail.skuName}】出库数量不能为空`, { icon: 2 });
            flag = true;
            return false;
          }
        });
        // 控制不能提交为空的数据
        if (flag) {
          return false;
        }
        d.field.receiptRecordDetailList = detailList;
        // 提交计划
        admin.req(
          "enterprise/receiptRecord/admin/receiptRecordOther/add",
          JSON.stringify(d.field),
          function (res) {
            layer.closeAll("loading");
            if (res.code == 200) {
              // 提交成功清空缓存数据
              materialData = [];
              layer.msg(res.msg, { icon: 1 });
              layer.closeAll();
              insTb.reload("receiptRecordTable");
            } else {
              layer.msg(res.msg, { icon: 2 });
            }
          },
          "POST",
          true,
          "application/json; charset=utf-8"
        );
        return false;
      });
    }
  );
</script>
<style>
  .addReceiptRecordDetailForm-title {
    width: 100%;
    margin-left: 20px;
  }

  .addReceiptRecordDetailForm-title .title,
  .title {
    display: inline-block;
    font-size: 16px;
    font-weight: bold;
    color: #333333;
    line-height: 19px;
    position: relative;
    padding: 6px 0 19px 0;
  }

  .addReceiptRecordDetailForm-title .title:after,
  .title:after {
    content: "";
    display: block;
    position: absolute;
    left: 0;
    bottom: 0;
    width: calc(100% + 6px);
    height: 3px;
    background: #0a8b0a;
  }

  .addTable {
    height: 30px;
    margin-left: 30px;
    line-height: 30px;
    padding: 0 10px;
  }
</style>
